package io.github.leon406.ext

import android.graphics.drawable.GradientDrawable
import android.view.View

fun GradientDrawable.fillColor(fillColor: Any) = apply { setColor(fillColor.toColor()) }

fun GradientDrawable.stroke(strokeColor: Any, strokeWidth: Int) = apply {
    setStroke(strokeWidth, strokeColor.toColor())
}

fun GradientDrawable.radius(radius: Float) = apply { cornerRadius = radius }

fun GradientDrawable.radius(
    topLeftRadius: Float,
    topRightRadius: Float,
    bottomRightRadius: Float,
    bottomLeftRadius: Float
) = apply {
    /// 设置图片四个角圆形半径：1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
    cornerRadii =
        floatArrayOf(
            topLeftRadius,
            topLeftRadius,
            topRightRadius,
            topRightRadius,
            bottomRightRadius,
            bottomRightRadius,
            bottomLeftRadius,
            bottomLeftRadius
        )
}

fun GradientDrawable.dash(
    strokeWidth: Int,
    dashColor: Any,
    dashWidth: Int,
    dashGap: Int,
    shapeType: Int = GradientDrawable.LINE
) = apply {
    setStroke(strokeWidth, dashColor.toColor(), dashWidth.toFloat(), dashGap.toFloat())
    shape = shapeType
}

fun GradientDrawable.gradient(
    startColor: Any,
    endColor: Any,
    orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.LEFT_RIGHT,
) = apply {
    this.orientation = orientation
    colors = intArrayOf(startColor.toColor(), endColor.toColor())
}

fun GradientDrawable.bind(target: View) {
    target.background = this
}
